package nl.rug.ml.dobbelen.game;

import java.util.LinkedList;
import java.util.List;

import nl.rug.ml.dobbelen.learning.DobbelenState;

/**
 * @author Do Bich Ngoc (bichngocdo89@gmail.com)
 * 
 */
public class DobbelenCondition {

	public static boolean mustReThrow(List<Dice> dices) {
		return DobbelenDiceScorer.calculateScore(dices) < 350;
	}

	public static boolean mustReThrow(DobbelenState state) {
		return DobbelenDiceScorer.calculateScore(state) < 350;
	}

	public static boolean mustThrowAll(List<Dice> dices) {
		boolean condition = true;
		List<Dice> checkingDices = new LinkedList<Dice>();
		checkingDices.addAll(dices);

		for (Dice dice : dices) {
			checkingDices.remove(dice);
			condition = condition && (DobbelenDiceScorer.calculateScore(checkingDices) < DobbelenDiceScorer.calculateScore(dices));
			checkingDices.add(dice);
			if (!condition) {
				return false;
			}
		}

		return condition;
	}

	public static boolean mustThrowAll(DobbelenState state) {
		boolean condition = true;
		DobbelenState clonedState = state.clone();
		int[] extendedHistogram = clonedState.getExtendedHistogram();

		for (int i = 0; i < extendedHistogram.length; i++) {
			if (extendedHistogram[i] > 0) {
				extendedHistogram[i]--;
				condition = condition && (DobbelenDiceScorer.calculateScore(clonedState) < DobbelenDiceScorer.calculateScore(state));
				extendedHistogram[i]++;
				if (!condition) {
					return false;
				}
			}
		}

		return condition;
	}

	public static boolean newDicesYeldNoScore(List<Dice> dices, List<Dice> heldDices) {
		if (DobbelenDiceScorer.calculateScore(dices) == DobbelenDiceScorer.calculateScore(heldDices)) {
			return true;
		}
		else {
			return false;
		}
	}

}
